require "test/unit"

class WhenCreatingNewFramework < Test::Unit::TestCase

  def setup
    @framework_path = 'framework_path'
    @framework_sdk_path = 'framework_sdk_path'
    @framework_version = 'framework_version'
    @framework_key = 'framework_key'
    @framework_sdk_key = 'framework_sdk_key'
    @framework_config = StubbedFrameworkConfig.new(@framework_path, @framework_sdk_path, @framework_key, @framework_sdk_key, @framework_version)
    @framework_dir = 'framework_dir'
    @framework_sdk_dir = 'framework_sdk_dir'
    @reg_path = mock()
  end

  def test_should_load_framework_dir_from_registry
 
    Win32::Registry::HKEY_LOCAL_MACHINE.expects(:open).with(@framework_path).returns(@reg_path)
    Win32::Registry::HKEY_LOCAL_MACHINE.stubs(:open).with(@framework_sdk_path).returns(@reg_path)
    @reg_path.expects(:read_s).with(@framework_key).returns(@framework_key)
    @reg_path.stubs(:read_s).with(@framework_sdk_key).returns(@framework_sdk_key)

    Rubicant::DotNet::Framework.new(@framework_config)
  end

  def test_should_load_framework_sdk_dir_from_registry

    Win32::Registry::HKEY_LOCAL_MACHINE.expects(:open).with(@framework_path).returns(@reg_path)
    Win32::Registry::HKEY_LOCAL_MACHINE.stubs(:open).with(@framework_sdk_path).returns(@reg_path)
    @reg_path.stubs(:read_s).with(@framework_key).returns(@framework_key)
    @reg_path.expects(:read_s).with(@framework_sdk_key).returns(@framework_sdk_key)

    Rubicant::DotNet::Framework.new(@framework_config)
  end

  def test_should_get_values_loaded_from_registry_from_created_framework
    Win32::Registry::HKEY_LOCAL_MACHINE.stubs(:open).with(@framework_path).returns(@reg_path)
    Win32::Registry::HKEY_LOCAL_MACHINE.stubs(:open).with(@framework_sdk_path).returns(@reg_path)
    @reg_path.stubs(:read_s).with(@framework_key).returns(@framework_dir)
    @reg_path.stubs(:read_s).with(@framework_sdk_key).returns(@framework_sdk_dir)

    result = Rubicant::DotNet::Framework.new(@framework_config)
    assert_equal "#{@framework_dir}\\#{@framework_version}", result.framework_dir
    assert_equal @framework_sdk_dir, result.sdk_dir
  end

end

class StubbedFrameworkConfig
  attr_reader :framework_registry_key, :framework_sdk_registry_key, :framework_registry_path, :framework_sdk_registry_path, :framework_version

  def initialize(framework_registry_path, framework_sdk_registry_path, framework_key, framework_sdk_key, framework_version)
    @framework_registry_path = framework_registry_path
    @framework_sdk_registry_path = framework_sdk_registry_path
    @framework_registry_key = framework_key
    @framework_sdk_registry_key = framework_sdk_key
    @framework_version = framework_version
  end
end
